package com.bkhc.order.api;

import com.bkhc.common.annotation.ApiOperation;
import com.bkhc.common.annotation.ApiPackage;
import com.bkhc.common.annotation.ApiRestController;
import com.bkhc.common.annotation.AuditLog;
import com.bkhc.common.entity.User;
import com.bkhc.common.enu.OperationEnum;
import com.bkhc.common.util.response.PageUtil;
import com.bkhc.common.util.response.ResultJson;
import com.bkhc.common.vo.OrderVo;
import com.bkhc.order.entity.Order;
import com.bkhc.order.entity.OrderItem;
import com.bkhc.order.service.OrderService;
import com.bkhc.order.vo.OrderQuery;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;
import java.io.IOException;
import java.util.List;

/**
 * @author pcq
 * @date 2021/6/20 17:57
 */
@RestController
@RequestMapping("/order")
@Api("订单管理服务")
@ApiPackage(code="order-api",name="订单管理服务",description = "后台订单管理维护操作接口")
public class OrderController extends CommonController {
    @Autowired
    private OrderService orderSerivce;

    @ApiOperation(code="queryOrder",name="查询订单",description = "查询订单",type = "common")
    @AuditLog(object="Order",operation = "查询订单",type = OperationEnum.query)
    @RequestMapping(path = "/list",method = RequestMethod.POST)
    public ResultJson<?> getOrders(@RequestBody(required=false) Order order,
                                   @RequestParam(name="pageIndex") int pageIndex,
                                   @RequestParam(name="pageSize") int pageSize) {
        ResultJson<?> res = orderSerivce.queryOrders(order, pageIndex, pageSize);
        return res;
    }

    @ApiOperation(code="queryOrderByUserId",name="登陆用户查询订单",description = "登陆用户查询订单",type = "common")
    @AuditLog(object="Order",operation = "查询订单",type = OperationEnum.query)
    @RequestMapping(path = "/queryOrderByUserId",method = RequestMethod.POST)
    public PageUtil queryOrderByUserId(@RequestBody Order order,
                                            @RequestParam(name="pageIndex") int pageIndex,
                                            @RequestParam(name="pageSize") int pageSize) {
        PageUtil res = orderSerivce.queryOrderByUserId(order,pageIndex, pageSize);
        return res;
    }

    @ApiOperation(code="queryOrderByProductor",name="",description = "产品合伙人查看订单",type = "common")
    @AuditLog(object="Order",operation = "产品合伙人查看订单",type = OperationEnum.query)
    @RequestMapping(path = "/list/productor",method = RequestMethod.POST)
    public PageUtil getOrdersByproductor(@RequestBody OrderQuery orderQuery,
                                         @RequestParam(name="pageIndex") int pageIndex,
                                         @RequestParam(name="pageSize") int pageSize) {
        PageUtil res = orderSerivce.getOrdersByproductor(orderQuery, pageIndex, pageSize);
        return res;
    }

    @ApiOperation(code="queryOrderByPartner",name="",description = "厂商查看订单",type = "common")
    @AuditLog(object="Order",operation = "厂商查看订单",type = OperationEnum.query)
    @RequestMapping(path = "/list/partner",method = RequestMethod.POST)
    public PageUtil queryOrderByPartner(@RequestBody OrderQuery orderQuery,
                                         @RequestParam(name="pageIndex") int pageIndex,
                                         @RequestParam(name="pageSize") int pageSize) {
        PageUtil res = orderSerivce.queryOrderByPartner(orderQuery, pageIndex, pageSize);
        return res;
    }

    @ApiOperation(code="queryOrderByBusiness",name="",description = "业务系统查看订单",type = "common")
    @AuditLog(object="Order",operation = "业务系统查看订单",type = OperationEnum.query)
    @RequestMapping(path = "/list/business",method = RequestMethod.POST)
    public PageUtil getOrdersByproductor(@RequestBody Order order,
                                         @RequestParam(name="pageIndex") int pageIndex,
                                         @RequestParam(name="pageSize") int pageSize) {
        PageUtil res = orderSerivce.getOrdersByBussiness(order, pageIndex, pageSize);
        return res;
    }

    @ApiOperation(code="queryOrderByUser",name="",description = "用户查看订单",type = "common")
    @AuditLog(object="Order",operation = "用户查看订单",type = OperationEnum.query)
    @RequestMapping(path = "/list/queryOrderByUser",method = RequestMethod.POST)
    public PageUtil queryOrderByUser(@RequestBody User user,
                                         @RequestParam(name="pageIndex") int pageIndex,
                                         @RequestParam(name="pageSize") int pageSize) {
        PageUtil res = orderSerivce.queryOrderByUser(user, pageIndex, pageSize);
        return res;
    }

    @ApiOperation(code="queryOrderItem",name="查询订单Item",description = "查询订单Item",type = "common")
    @AuditLog(object="Order",operation = "查询订单Item",type = OperationEnum.query)
    @RequestMapping(path = "/item",method = RequestMethod.POST)
    public ResultJson<List<OrderItem>> getOrders(@RequestParam(name="orderCode") String orderCode) {
        ResultJson<List<OrderItem>> res = orderSerivce.queryOrderItem(orderCode);
        return res;
    }

    @ApiOperation(code="addOrder",name="增加订单",description = "增加订单", type = "common")
    @AuditLog(object="Order",operation = "增加订单",type = OperationEnum.add)
    @RequestMapping(path = "/add",method = RequestMethod.POST)
    public ResultJson<?> addOrder(@RequestBody(required=true) Order order) {
        ResultJson<?> res = orderSerivce.addOrder(order);
        return res;
    }

    @ApiOperation(code="updateOrderStatus",name="",description = "产品合伙人修改订单状态",type = "common")
    @AuditLog(object="Order",operation = "产品合伙人查看订单",type = OperationEnum.query)
    @RequestMapping(path = "/{orderCode}/updateOrderStatus/{orderStatus}",method = RequestMethod.POST)
    public ResultJson<?> updateOrderStatusByProductor(@PathVariable(name="orderCode")String orderCode,
                                                 @PathVariable(name="orderStatus")String orderStatus) {
        ResultJson<?> res = orderSerivce.updateOrderStatusByProductor(orderCode,orderStatus);
        return res;
    }

    @ApiOperation(code="updateOrderAuditStatus",name="",description = "修改订单提交状态",type = "common")
    @AuditLog(object="Order",operation = "修改订单提交状态",type = OperationEnum.update)
    @RequestMapping(path = "/{orderCode}/updateOrderAuditStatus/{auditStatus}",method = RequestMethod.POST)
    public ResultJson<?> updateOrderAuditStatus(@PathVariable(name="orderCode")String orderCode,
                                                      @PathVariable(name="auditStatus")int auditStatus) {
        ResultJson<?> res = orderSerivce.updateOrderAuditStatus(orderCode,auditStatus);
        return res;
    }

    @ApiOperation(code="assginToPartner",name="",description = "产品合伙人指派给厂商的订单",type = "common")
    @AuditLog(object="assginToPartner",operation = "产品合伙人指派给厂商的订单",type = OperationEnum.update)
    @RequestMapping(path = "assgin/partner/{orderCode}",method = RequestMethod.POST)
    public ResultJson<?> assginToPartner(@PathVariable(name="orderCode")String orderCode
                                                ) {
        ResultJson<?> res = orderSerivce.assginToPartner(orderCode);
        return res;
    }

    @ApiOperation(code="downOrder",name="",description = "用户登录下载订单",type = "common")
    @AuditLog(object="Order",operation = "用户登录下载订单",type = OperationEnum.query)
    @RequestMapping(path = "/downOrder",method = RequestMethod.POST)
    public void downloadOrder(Order order, HttpServletResponse response) {
        orderSerivce.downloadOrder(order,response);
    }
}
